#include <soar.h>

void _stdin(unsigned int algm, char *data)
{/*{{{*/
    unsigned char value[64];
    int value_size;
    int result;
    int i;

    if((result=ossl_digest(algm, (unsigned char *)data, strlen(data),
                    value, &value_size)))
    {
        printf("ossl_digest出错，错误码:%d\n", result);
        return;
    }
    for(i=0; i<value_size; i++)
        printf("%02X", value[i]);
    printf("\n");

    return;    
}/*}}}*/

void _file(unsigned int algm, char *filename)
{/*{{{*/
    unsigned char value[64];
    int value_size;
    int result;
    int i;

    if((result=ossl_digest_file(algm, filename,
                    value, &value_size)))
    {
        printf("ossl_digest_file出错，错误码:%d\n", result);
        return;
    }
    for(i=0; i<value_size; i++)
        printf("%02X", value[i]);
    printf("\n");

    return;    
}/*}}}*/

int main(int argc, char *argv[])
{/*{{{*/
    char algm_desc[10];
    unsigned int algm;
    char plain[200];

    if(argc > 1)
    {
        if(strcasecmp(argv[1], "md5") == 0)
        {
            strcpy(algm_desc, argv[1]);
            algm=OSSL_DIGEST_MD5;
        }
        if(strcasecmp(argv[1], "sha1") == 0)
        {
            strcpy(algm_desc, argv[1]);
            algm=OSSL_DIGEST_SHA1;
        }
        else
        {
            strcpy(algm_desc, "md5");
            algm=OSSL_DIGEST_MD5;
        }
    }
    else
    {
        strcpy(algm_desc, "md5");
        algm=OSSL_DIGEST_MD5;
    }

    ossl_init();

    if(argc != 3)
    {
        while(fgets(plain, sizeof(plain), stdin))
        {
            if(plain[0] == RTN) break;
            _stdin(algm, plain);
        }
    }
    else
    {
        _file(algm, argv[1]);
    }

    ossl_clean();

    return(0);
}/*}}}*/
